//
//  SheetCapitalView.swift
//  Exchange
//
//  Created by 孟祥群 on 2018/5/7.
//  Copyright © 2018年 common. All rights reserved.
//

import UIKit

class SheetCapitalView: UIView, UICollectionViewDelegate, UICollectionViewDataSource {
    var config:CapitalConfig?
    var model:CapitalModel?
    var dataSource:SelectmangerDelegate?
    var collectionView:UICollectionView?
    
    init(config:SelectConfig ,model:SheetModel ,dataSource:SelectmangerDelegate) {
      super.init(frame: CGRect.zero)
      self.dataSource = dataSource
        self.config = config as? CapitalConfig
        self.model = model as? CapitalModel
      self.setUpUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func setUpUI() -> Void {
        let layout = UICollectionViewFlowLayout()
        layout.scrollDirection = .vertical
        layout.minimumLineSpacing = 20
        layout.itemSize = CGSize(width: (kSCREEN_WIDTH - 90)/3, height: 40)
        layout.sectionInset = UIEdgeInsetsMake(0, 25, 0, 25)
        
        let collectionView = UICollectionView(frame: CGRect.zero, collectionViewLayout: layout)
        self.collectionView = collectionView
        collectionView.showsVerticalScrollIndicator = false
        collectionView.showsHorizontalScrollIndicator = false
        collectionView.delegate = self
        collectionView.dataSource = self
        collectionView.themeBackground = ThemeManager.ThemeColorKeys.background
        collectionView.register(LeverSelectCollectionViewCell.self, forCellWithReuseIdentifier: "identifier")
        self.addSubview(collectionView)
        collectionView.register(UICollectionReusableView.self, forSupplementaryViewOfKind: UICollectionElementKindSectionHeader, withReuseIdentifier: "collettionSectionHeader")
        collectionView.snp.makeConstraints { (make) in
            make.top.bottom.right.left.equalToSuperview()
        }
    }
    
    func numberOfSections(in collectionView: UICollectionView) -> Int {
        return 1
    }
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        
        return (model?.titleArr.count)!
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell:LeverSelectCollectionViewCell = collectionView.dequeueReusableCell(withReuseIdentifier: "identifier", for: indexPath) as! LeverSelectCollectionViewCell
        cell.tag = KCellTag*indexPath.section + indexPath.row
        cell.titleLabel.text = model?.titleArr[indexPath.row]
        cell.titleLabel.font = UIFont.systemFont(ofSize: 13)
        cell.titleLabel.textAlignment = NSTextAlignment.center

//        }
        if model?.curretnStr == cell.titleLabel.text {
            cell.backgroundColor = kThemeColor
            cell.titleLabel.textColor = UIColor.white
            cell.titleLabel.layer.borderWidth = 0
        }else {
            cell.titleLabel.layer.borderWidth = 0.5
            cell.titleLabel.themeBackground = ThemeManager.ThemeColorKeys.background
            cell.titleLabel.themeTextColor = ThemeManager.ThemeColorKeys.text2
            cell.titleLabel.layer.borderColor = cell.titleLabel.textColor.cgColor
        }
        
        return cell
    }
    
    func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
        let view = collectionView.dequeueReusableSupplementaryView(ofKind: UICollectionElementKindSectionHeader, withReuseIdentifier: "collettionSectionHeader", for: indexPath)
        return view
    }

    
    func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> CGSize {
        return CGSize(width: kSCREEN_WIDTH, height: 114)
    }
    
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        model?.selectStr = model?.titleArr[indexPath.row]
        dataSource?.selectModel(model: model!)
        SelectActionManger.manager.close()
        
    }
    
    
    /*
    // Only override draw() if you perform custom drawing.
    // An empty implementation adversely affects performance during animation.
    override func draw(_ rect: CGRect) {
        // Drawing code
    }
    */

}
